package com.lovo.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.project.bean.SupplierBean;
import com.lovo.project.service.SupplierService;
import com.lovo.project.util.FileUtils;
import com.lovo.project.util.Jiexi;
import jdk.jshell.execution.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    SupplierService supplierService;

    @RequestMapping("/querySupplierList")
    public Page<SupplierBean> querySupplierList(String supplierName, String supplierType, Integer pg, Integer pgsize) {
        Page<SupplierBean> page = new Page<>(pg, pgsize);
        QueryWrapper<SupplierBean> queryWrapper = new QueryWrapper<>();
        if (supplierName != null && supplierName != "") {
            queryWrapper.like("supplier_name", supplierName);
        }
        if (supplierType != null && supplierType != "") {
            queryWrapper.like("supplier_type", supplierType);
        }
        Page<SupplierBean> pageInfo = supplierService.querySupplierList(page, queryWrapper);
        return pageInfo;
    }

    @RequestMapping("/querySupplier")
    public SupplierBean querySupplier(Integer supplierId) {
        SupplierBean supplierBean = supplierService.querySupplier(supplierId);
        return supplierBean;
    }

    @RequestMapping("/updateSupplier")
    public String updateSupplier(Integer supplierId, String supplierDuty, String supplierTel) {
        SupplierBean supplierBean = new SupplierBean();
        supplierBean.setSupplierId(supplierId);
        supplierBean.setSupplierDuty(supplierDuty);
        supplierBean.setSupplierTel(supplierTel);
        supplierService.updateSupplier(supplierBean);
        return "ok";
    }

    @RequestMapping("/addSupplier")
    public String addSupplier(String supplierName, String supplierDuty, String supplierTel, String supplierAddress, String supplierType) {
        SupplierBean supplierBean = new SupplierBean();
        supplierBean.setSupplierName(supplierName);
        supplierBean.setSupplierDuty(supplierDuty);
        supplierBean.setSupplierTel(supplierTel);
        supplierBean.setSupplierAddress(supplierAddress);
        supplierBean.setSupplierType(supplierType);
        supplierService.insertSupplier(supplierBean);
        return "ok";
    }

    @RequestMapping("/querySupplierNameList")
    public List<String> querySupplierNameList() {
        List<SupplierBean> list = supplierService.querySupplierNameList(null);
        List<String> list1 = new ArrayList<>();
        for (SupplierBean b : list) {
            list1.add(b.getSupplierName());
        }
        return list1;
    }

    @RequestMapping("/UploadFiles")
    public String uploadFileAbsolute(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        byte[] bytes = multipartFile.getBytes();
        InputStream inputStream = new ByteArrayInputStream(bytes);
        // 解析数据
        List<LinkedHashMap<String, Object>> maps = Jiexi.xlsxImportExcel(inputStream, 0, 1, 0);
        SupplierBean supplierBean = new SupplierBean();
        for (LinkedHashMap b : maps) {
            supplierBean.setSupplierName((String) b.get("供应商名称"));
            supplierBean.setSupplierDuty((String) b.get("负责人"));
            supplierBean.setSupplierTel((String) b.get("负责人电话"));
            supplierBean.setSupplierAddress((String) b.get("供应商地址"));
            supplierBean.setSupplierType((String) b.get("经营范围"));
            supplierService.insertSupplier(supplierBean);
        }
        return "ok";
    }
}
